package com.itheima.project.service;

import com.itheima.project.entity.User;
import com.itheima.project.entity.UserAuth;
import com.itheima.project.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在或已被禁用");
        }
        UserAuth userAuth = new UserAuth();
        userAuth.setUsername(user.getUsername());
        userAuth.setPassword(user.getPassword());
        userAuth.setNickName(user.getNickName());

        //添加角色
        List<String> roles = new ArrayList<>();
        if ("user@qq.com".equals(username)) {
            roles.add("USER");
            userAuth.setRoles(roles);
        }
        if ("admin@qq.com".equals(username)) {
            roles.add("USER");
            roles.add("ADMIN");
            userAuth.setRoles(roles);
        }
        return userAuth;
    }


}

/*改造UserDetailsServiceImpl version 2.0
 *//**
 * 基于JDBC数据库实现认证
 * 提供用户详细信息
 * 可以把SecurityConfig注释了UserDetailsService部分
 *
 * @param username
 * @return
 * @throws UsernameNotFoundException
 *//*
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if(username.equals("user")){
            UserDetails user= User.builder()
                    .username("user")
                    .password("$2a$10$ODTyhKD7XpJLDiwLvr8.CuyhHt/9h6GOcpYptizFFwsFvWPE3hyJ2")
                    .roles("USER")
                    .build();
            return user;
        }
        if(username.equals("admin")){
            UserDetails admin= User.builder()
                    .username("admin")
                    .password("$2a$10$1jHD5J86b1Bxeh2ytQFj4OEuyJ9U1RkpyigxRlnb1VebsZHYM3oLi")
                    .roles("ADMIN","USER")
                    .build();
            return admin;
        }
        return null;
    }*/


/*
    //注释掉，更新代码  version 2.0

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //查询用户
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在或已被禁用");
        }
        SimpleGrantedAuthority user_role = new SimpleGrantedAuthority("user");
        SimpleGrantedAuthority admin_role = new SimpleGrantedAuthority("admin");
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();

        list.add(user_role);
        list.add(admin_role);

        return new org.springframework.security.core.userdetails.User(user.getUsername()
                , user.getPassword()
                , list);
    }*/
